#pragma once

#include "Business/ClassicArbitrageContracts.h"
#include "Business/CustomerPosition.h"

class ClassicBusinessPosition
{
public:
	explicit ClassicBusinessPosition(ArbitrageContracts const& contracts,PositionValue const& majorPosition, PositionValue const& minorPosition)
		:m_contracts(contracts),
		m_majorPosition(majorPosition),m_minorPosition(minorPosition)
	{
		InitPositionPointer();
	}

	explicit ClassicBusinessPosition(ArbitrageContracts const& contracts)
		:m_contracts(contracts),
		m_majorPosition(0.00,0),m_minorPosition(0.00,0)
	{
		InitPositionPointer();
	}

	ClassicBusinessPosition(ClassicBusinessPosition const& other)
		:m_contracts(other.m_contracts),
		m_majorPosition(other.m_majorPosition),m_minorPosition(other.m_minorPosition)
	{
		InitPositionPointer();
	}


	PositionValue const& GetMajor() const { return m_majorPosition; }
	PositionValue const& GetMinor() const { return m_minorPosition; }
	PositionValue const& GetFarther() const { return *m_fartherPosition; }
	PositionValue const& GetNearby() const { return *m_nearbyPosition; }

	bool IsZero() const
	{
		return GetMajor().GetQuantity() == 0 && GetMinor().GetQuantity() == 0;
	}

	void Update(int contract, Operation::TYPE operation,PositionValue const& position)
	{
		PositionValue& p = (contract == m_contracts.GetMajor()) ? m_majorPosition : m_minorPosition;
		if (operation == Operation::OPEN)
			p.Plus(position);
		else
			p.Sub(position);
	}

	void Clear()
	{
		m_majorPosition = PositionValue(0.00,0);
		m_minorPosition = PositionValue(0.00,0);
	}
private:
	void InitPositionPointer()
	{
		if (m_contracts.GetMajor() == m_contracts.GetFarther())
		{
			m_fartherPosition = &m_majorPosition;
			m_nearbyPosition = &m_minorPosition;
		}
		else
		{
			m_fartherPosition = &m_minorPosition;
			m_nearbyPosition = &m_majorPosition;
		}
	}
	ArbitrageContracts m_contracts;
	PositionValue m_majorPosition;
	PositionValue m_minorPosition;
	PositionValue* m_fartherPosition;
	PositionValue* m_nearbyPosition;

	ClassicBusinessPosition& operator==(ClassicBusinessPosition const& other);

};
